package com.core.service;

import com.core.bean.Goods;
import com.core.bean.Receiving;
import com.core.bean.Shipments;
import com.core.bean.UserAddress;
import com.core.dao.*;
import com.core.dao.jdbcmysql.JdbcMysqlFactory;
import com.core.dao.jdbcmysql.ShipmentsDao;
import com.core.dao.mybatismysql.GoodsDao;
import com.core.dao.mybatismysql.MybatisMysqlFactory;
import com.core.dao.mybatismysql.ReceivingDao;
import com.core.dao.mybatismysql.UserAddressDao;
import com.core.utils.UUIDTool;
import com.core.vo.ShipmentsVO;

public class ShipmentsService {


    private final int STATUS = 1;
    private final int REJECT_STATUS = 0;
    private final int CONFIRM_STATUS = 1;

    public int add(ShipmentsVO shipmentsVO) throws Exception {
        IShipmentsDao shipmentsDao = new ShipmentsDao();
        Shipments shipments = new Shipments();
        try {
            IGoodsDao goodsDao = new GoodsDao();
            Goods goods = goodsDao.findEntityById(shipmentsVO.getGoodsId());
            if (goods == null) {
                throw new RuntimeException();
            }
            IUserAddressDao userAddressDao = new UserAddressDao();
            UserAddress userAddress = userAddressDao.findEntityById(shipmentsVO.getAddrId());
            if (userAddress == null) {
                throw new RuntimeException();
            }
            String uuid = UUIDTool.getUUID();
            shipments.setId(uuid);
            shipments.setGoods(goods);
            shipments.setConsignor(shipmentsVO.getConsignor());
            shipments.setShipAddress(shipmentsVO.getShipAddress());
            shipments.setUserAddress(userAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
        shipments.setStatus(STATUS);
        return shipmentsDao.add(shipments);

    }


    /**
     * 拒接签收
     * <p>
     * 1.查询传进来的id是否存在
     * 2.插入收货表
     *
     * @param id 发货单id
     * @return
     * @throws Exception
     */
    public int Reject(String id) throws Exception {
        Shipments shipments = this.selectById(id);
        if (shipments == null) {
            throw new RuntimeException();
        }
        IReceivingDao receivingDao = new ReceivingDao();
        String uuid = UUIDTool.getUUID();
        int add = receivingDao.add(new Receiving(uuid, shipments.getId(), REJECT_STATUS));
        return add;
    }

    /**
     * 1.查询传进来的id是否存在
     *
     * @param id
     * @return
     * @throws Exception
     */
    public int confirm(String id) throws Exception {
        Shipments shipments = this.selectById(id);
        if (shipments == null) {
            throw new RuntimeException();
        }
        IReceivingDao receivingDao = new ReceivingDao();
        String uuid = UUIDTool.getUUID();
        int add = receivingDao.add(new Receiving(uuid, shipments.getId(), CONFIRM_STATUS));
        return add;
    }

    /**
     * 查询传进来的id是否存在
     *
     * @return
     */
    public Shipments selectById(String id) throws Exception {
        IShipmentsDao shipmentsDao = new ShipmentsDao();
        return shipmentsDao.findEntityById(id);

    }
}
